<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class TeamUserModel extends Model
{
	
	/**
	 * 获取用户参加的团队IDs
	 *
	 * @param int $userID
	 * @return  array (只包含team_id的一维数组)
	 */
	public function getUserTeamIDs($userID)
	{
		$teams = $results = array();
		$map = array('user_id' => $userID);
		$results = $this->where($map)->select();
		if (!empty($results))
		{
			foreach ($results as $value)
			{
				$teams[] = $value['team_id'];
			}
		}
		return $teams;
	}
	
	
	/**
	 * 获取用户参加的团队
	 *
	 * @param int $userID
	 * @return  array
	 */
	public function getUserTeams($userID)
	{
		$teams = array();
		$prefix = $this->tablePrefix;
		$sql = "SELECT T.* FROM {$prefix}team T,{$prefix}team_user TU WHERE T.team_id=TU.team_id AND TU.user_id={$userID}";
		$teams = $this->query($sql);
		return $teams;
	}
	
	
	/**
	 * 获取团队成员数
	 * 
	 * @param int $teamID
	 * @return int
	 */
	public function getTeamUserCount($teamID)
	{
		$map = array(
			'team_id' => $teamID,
			'status' => 1,
		);
		$count = $this->where($map)->count();
		return intval($count);
	}
	
	
	/**
	 * 检查用户是否是某团队成员
	 *
	 * @param int $teamID
	 * @param int $userID
	 * @return bool
	 */
	public function isTeamUser($teamID, $userID)
	{
		$map = array(
			'team_id' => $teamID, 
			'user_id' => $userID,
			'status'  => 1,
		);
		$count = $this->where($map)->count();
		return $count < 1 ? false : true;
	}
	
	
	/**
	 * 审核通过入团
	 *
	 * @param int $teamID
	 * @param int $userID
	 * @return mix
	 */
	public function approveUser($teamID, $userID)
	{
		$where = array(
			'team_id' => $teamID, 
			'user_id' => $userID,
		);
		$this->where($where)->setField('status', 1);
	}
	
	
	/**
	 * 审核拒绝入团
	 *
	 * @param int $teamID
	 * @param int $userID
	 * @return mix
	 */
	public function disapproveUser($teamID, $userID)
	{
		$where = array(
			'team_id' => $teamID, 
			'user_id' => $userID,
		);
		$this->where($where)->setField('status', -1);
	}
	
	
	/**
	 * 删除团队成员
	 *
	 * @param int $teamID
	 * @param int $userID
	 * @return null
	 */
	public function deleteUser($teamID, $userID)
	{
		$where = array(
			'team_id' => $teamID,
			'user_id' => $userID,
		);
		$this->where($where)->delete();
	}
	
}

/* End of file TeamUserModel.class.php */
/* Location: ./App/Lib/Model/TeamUserModel.class.php */